<?php
namespace app\index\controller;
use think\Db;
class Listlive extends Ajax
{
    protected function initialize()
    {
        parent::initialize();
        if (empty($this->_user)) {
            $this->success('请登录', '/index/index/index');
        }
    }

    public function index()
    {
        return view('index');
    }

    public function startLive()
    {
        $user = $this->_user;
        $bool = M('LiveInfo')->where(array('user_id'=>$user['id']))->update(['status' => '1']);
        $data = M('LiveInfo')->getDataByField(array('user_id'=>$user['id']));
        $this->_data['data'] = $data[0];
        $this->echoJson();
    }
    public function stopLive()
    {
        $user = $this->_user;
        $bool = M('LiveInfo')->where(array('user_id'=>$user['id']))->update(['status' => '0']);
        $this->echoJson();
    }
    public function ListsInfo() {
        $data = M('LiveInfo')->where(array('status'=>0))->order('ctime desc')->select();
        foreach ($data as $key => $value) {
                $user_ids[] = $value['user_id'];
        }
        $user_info  = M('User')->where('id','in',$user_ids)->select();
        foreach ($user_info as $key => $value) {
            $user_infos[$value['id']] =$value;
        }
        foreach ($data as $key => $value) {
            $data[$key]['name'] = $user_infos[$value['user_id']]['name'];
        }
        $this->_data['data'] = $data;
        $this->echoJson();
    }
}
